package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品操作")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品操作
     */
    @PostMapping
    @ApiOperation("菜品添加")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("菜品参数{}", dishDTO);
        dishService.save(dishDTO);
        //清除缓存
        String key="dish_"+dishDTO.getId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> queryPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品查询参数{}", dishPageQueryDTO);
        PageResult pageResult = dishService.queryPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDishes(@RequestParam List<Long> ids) {
        log.info("批量删除菜品{}", ids);
        dishService.deleteDishes(ids);
        //删除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id查找菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查找菜品")
    public Result<DishVO> queryById(@PathVariable Long id) {
        log.info("根据id查找菜品{}", id);
        DishVO dishVO = dishService.queryById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateWithFlavors(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品{}", dishDTO);
        dishService.updateWithFlavors(dishDTO);
        //删除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查找菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查找菜品")
    public Result<List<Dish>> categoryIdFindDish(Long categoryId) {
        log.info("分类id{}", categoryId);
        List<Dish> list = dishService.categoryIdFindDish(categoryId);
        return Result.success(list);
    }

    /**
     * 菜品起售和停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售和停售")
    public Result onOrOff(@PathVariable Integer status,Long id) {
        dishService.onOrOff(status,id);
        //删除缓存
        cleanCache("dish_*");
        return Result.success();
    }
    /**
     * 清理缓存
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
